package com.lxw.ObserverPaterns.Subject;

import com.lxw.ObserverPaterns.Observer.Observer;

import java.util.ArrayList;


/**
 * 继承主题接口的类实现了
 * 注册观察者
 * 移除观察者
 * 唤醒观察者
 * 以及本身的状态变更方法
 *
 */
public class WeatherData implements Subject {
    private ArrayList observers;
    private double temperature;
    private double humidity;
    private double pressure;

    public WeatherData(){
        observers=new ArrayList();
    }


    public void registerObserver(Observer o) {
        observers.add(o);
    }

    public void removeObserver(Observer o) {
        int i=observers.indexOf(o);
        if(i>0){
            observers.remove(i);
        }
    }

    public void notifyObservers() {
        for (int i=0;i<observers.size();i++){
            Observer observer=(Observer)observers.get(i);
            observer.update(temperature,humidity,pressure);
        }
    }

    public void measurementsChanged(){
        notifyObservers();
    }

    public void setMeasurements(double temperature,double humidity,double pressure){
        this.temperature=temperature;
        this.humidity=humidity;
        this.pressure=pressure;
        measurementsChanged();
    }




}
